package levelrewind.android.app.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

import levelrewind.android.app.FacebookFragment;
import levelrewind.android.app.model.Util.Emotion;
import levelrewind.android.app.vo.BattleScore;
import levelrewind.android.app.vo.Category;
import levelrewind.android.app.vo.Game;
import levelrewind.android.app.vo.HighScore;
import levelrewind.android.app.vo.LatestPlayer;
import levelrewind.android.app.vo.Person;
import levelrewind.android.app.vo.PersonPlace;
import levelrewind.android.app.vo.PersonScore;
import levelrewind.android.app.vo.PicturePath;
import levelrewind.android.app.vo.ServerTime;
import levelrewind.android.app.vo.User;
import levelrewind.android.app.vo.UserScore;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.content.Context;
import android.util.Log;

@SuppressWarnings("unused")
public class SoapDataHandler {
	private Database db;
	private static SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd");
	
	public SoapDataHandler(Context c) {
		db = new Database(c);
	}
	public static Category[] getCategories()
	{
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultCategories"; 
		String OPERATION_NAME = "resultCategories";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetCategoriesWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        Category[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new Category[res.size()];
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

	            Category cat = new Category();
	            cat.setId(Integer.parseInt(so.getProperty(0).toString()));
	            cat.setName(so.getProperty(1).toString());
	            cat.setDescription(so.getProperty(2) == null? "" :so.getProperty(2).toString());
	            list[i] = cat;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}
	
	public static Person[] getPersons(int category_id)
	{
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultPersonList"; 
		String OPERATION_NAME = "resultPersonList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetPersonListByCategoryWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("catID", category_id);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        Person[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new Person[res.size()];
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

	            Person per = new Person();
	            per.setId(Integer.parseInt(so.getProperty(0).toString()));
	            per.setName(so.getProperty(1).toString());
	            per.setCategoryId(Integer.parseInt(so.getProperty(2).toString()));
	            per.setDescription(so.getProperty(3) == null? "" :so.getProperty(3).toString());
	            per.setScore(Long.parseLong(so.getProperty(4).toString()));
	            per.setNumOfFans(Long.parseLong(so.getProperty(5).toString()));
	            per.setIsCurrentBattle(Integer.parseInt(so.getProperty(6).toString()));
	            
	            @SuppressWarnings("rawtypes")
				Vector paths = (Vector)so.getProperty(7);
	            if (paths != null && paths.size() != 0) {
	            	Log.i("Download PicPath", "perID: "+ per.getId());
		            Map<Emotion, PicturePath> picMap = new HashMap<Emotion, PicturePath>();
		            
		            for (int j=0; j<paths.size();j++){
			            Object pobj = (Object)paths.get(j);
				        SoapObject  pso = (SoapObject)pobj;
				        
				        PicturePath pp = new PicturePath();
				        pp.setEmotionId(Integer.parseInt(pso.getProperty(0).toString()));
				        pp.setEmotionName(pso.getProperty(1).toString());
				        pp.setPath(pso.getProperty(2).toString());
				        
				        
				        Emotion emotion = Util.getEmotionByName(pp.getEmotionName());
				        picMap.put(emotion, pp);		
		            }
		            
		            per.setPicPath(picMap);	            
	            }
	            
	            list[i] = per;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }

        return list;
	}
	
	public static PicturePath[] getPersonPicturesPath(int person_id)
	{
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultList"; 
		String OPERATION_NAME = "resultList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetPersonPicturesPathWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("perID", person_id);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        PicturePath[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new PicturePath[res.size()];
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

		        PicturePath per = new PicturePath();
//	            per.setPersonId(Integer.parseInt(so.getProperty(0).toString()));
	            per.setEmotionId(Integer.parseInt(so.getProperty(1).toString()));
	            per.setPath(so.getProperty(2).toString());
	            list[i] = per;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}
	
	public static Game[] getGames()
	{
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultGames"; 
		String OPERATION_NAME = "resultGames";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetGamesWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        Game[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new Game[res.size()];
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

	            Game game = new Game();
	            game.setId(Integer.parseInt(so.getProperty(0).toString()));
	            game.setName( so.getProperty(1).toString());
	            game.setDescription(so.getProperty(2) == null? "" :so.getProperty(2).toString());
	            game.setGamePlayed(so.getProperty(3) == null ? 0 : Long.parseLong(so.getProperty(3).toString()));
	        	game.setPublished(so.getProperty(4) == null ? "" : so.getProperty(4).toString());
	        	game.setIsEmbedded(so.getProperty(5) == null ? 0 : Integer.parseInt(so.getProperty(5).toString()));
	        	game.setPlatform(so.getProperty(6) == null ? "" : so.getProperty(6).toString());
	        	game.setAppStoreLink(so.getProperty(7) == null ? "" : so.getProperty(7).toString());
	        	game.setGooglePlayLink(so.getProperty(8) == null ? "" : so.getProperty(8).toString());
	        	
	            list[i] = game;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}
	
	public static PersonScore[] getPersonScores(int person_id)
	{
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultPersonList"; 
		String OPERATION_NAME = "resultPersonList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetPersonScoresWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("perID", person_id);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        PersonScore[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new PersonScore[res.size()];
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

		        PersonScore s = new PersonScore();
	            s.setPersonId(Integer.parseInt(so.getProperty(0).toString()));
	            s.setWeek(so.getProperty(1).toString());
	            s.setScore(Long.parseLong(so.getProperty(2).toString()));
	        	
	            list[i] = s;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}
	
	public static HighScore[] getHighScoreListByPerson(int person_id)
	{
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultList"; 
		String OPERATION_NAME = "resultList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetHighScoreListByPersonWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("perID", person_id);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        HighScore[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new HighScore[res.size()];
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

		        HighScore hs = new HighScore();
	           // s.setPersonId(Integer.parseInt(so.getProperty(0).toString()));
		        
		        //User
				Object user = (Object)so.getProperty(0);
		        SoapObject  soapUser = (SoapObject)user;
	            User u = new User();
	            u.setId(Long.parseLong(soapUser.getProperty(0).toString()));
	            u.setEmail(soapUser.getProperty(1).toString());
	            u.setName(soapUser.getProperty(2) == null? null : soapUser.getProperty(2).toString());
	            
	            hs.setUser(u);
	            hs.setScore(Long.parseLong(so.getProperty(1).toString()));
	            
	            //Game
	            Object game = (Object)so.getProperty(2);
		        SoapObject  soapGame= (SoapObject)game;
	            Game g = new Game();
	            g.setId(Integer.parseInt(soapGame.getProperty(0).toString()));
	            g.setName(soapGame.getProperty(1).toString());
	            
	            hs.setGame(g);
	            list[i] = hs;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}
 
	public static LatestPlayer[] getLatestPlayer(int person_id)
	{
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultList"; 
		String OPERATION_NAME = "resultList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetLatestPlayerWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("perID", person_id);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        LatestPlayer[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new LatestPlayer[res.size()];
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

		        LatestPlayer lp = new LatestPlayer();
	           // s.setPersonId(Integer.parseInt(so.getProperty(0).toString()));
		        lp.setPersonId(person_id);
		        
		        //User
				Object user = (Object)so.getProperty(0);
		        SoapObject  soapUser = (SoapObject)user;
	            User u = new User();
	            u.setId(Long.parseLong(soapUser.getProperty(0).toString()));
	            u.setEmail(soapUser.getProperty(1).toString());
	            u.setName(soapUser.getProperty(2) == null? null : soapUser.getProperty(2).toString());
	            
	            lp.setUser(u);
	            lp.setScore(Long.parseLong(so.getProperty(1).toString()));
	            
	            //Game
	            Object game = (Object)so.getProperty(2);
		        SoapObject  soapGame= (SoapObject)game;
	            Game g = new Game();
	            g.setId(Integer.parseInt(soapGame.getProperty(0).toString()));
	            g.setName(soapGame.getProperty(1).toString());
	            
	            lp.setGame(g);
	            list[i] = lp;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}
	
	public static PersonPlace updatePersonScoreAndPlace(long user_id, int category_id, int person_id, int game_id, long score, Date date_played) {
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultList"; 
		String OPERATION_NAME = "resultList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/UpdatePersonScoreAndPlaceWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("userID", user_id);
		request.addProperty("catID", category_id);
		request.addProperty("perID", person_id);
		request.addProperty("gameID", game_id);
		request.addProperty("score", score);
		
		request.addProperty("dateplayed", sdf.format(date_played));
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        PersonPlace place = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
        	Object obj = (Object)response;
	        SoapObject  so = (SoapObject)obj;
	        
	        if (so != null) {
		        place = new PersonPlace();
		        place.setPersonId(person_id);
		        place.setPlaceBefore(Integer.parseInt(so.getProperty(0).toString()));
		        place.setPlaceNow(Integer.parseInt(so.getProperty(1).toString()));
		        place.setPointToFirstPlace(so.getProperty(2) == null ? null : Long.parseLong(so.getProperty(2).toString()));
		        place.setPointToTop5(so.getProperty(3) == null ? null : Long.parseLong(so.getProperty(3).toString()));
		        place.setPointTo1PlaceUp(so.getProperty(4) == null ? null : Long.parseLong(so.getProperty(4).toString()));
		        place.setTotalScore(so.getProperty(5) == null ? null : Long.parseLong(so.getProperty(5).toString()));
		        place.setScore(score);
	        }
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return place;
        
	}

	// TODO: Need to edit because Battles tables are changed
	/*public static Battle[] getBattles(Context context) {
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultList"; 
		String OPERATION_NAME = "resultList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetBattlesWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        Battle[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new Battle[res.size()];
	        
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

		        Battle battle = new Battle();
	            battle.setId(Integer.parseInt(so.getProperty(0).toString()));
	            battle.setName( so.getProperty(1).toString());
	            
	            Object person1 = (Object)so.getProperty(2);
		        SoapObject  soapPerson1 = (SoapObject)person1;
		        int perId = Integer.parseInt(soapPerson1.getProperty(0).toString());
		        Database db = new Database(context);
		        Person p1 = db.getPerson(perId);
	            battle.setPerson1(p1);
	            
	            Object person2 = (Object)so.getProperty(3);
		        SoapObject  soapPerson2 = (SoapObject)person2;
		        perId = Integer.parseInt(soapPerson2.getProperty(0).toString());
		        Person p2 = db.getPerson(perId);
	            battle.setPerson2(p2);
	            
	        	battle.setStartDate(sdf.parse(so.getProperty(4).toString()));
	        	
	        	battle.setSlogan(so.getProperty(6) == null ? "" : so.getProperty(6).toString());
	        	
	        	// Set the end date to stop at the last time of the day by add 1 more day
	        	// current format of EndDate = yyyy-MM-dd 00:00:00
	        	Date endDate = sdf.parse(so.getProperty(5).toString());
	        	Calendar c = Calendar.getInstance();
	        	c.setTime(endDate);
	        	c.add(Calendar.DAY_OF_YEAR, 1);
	        	battle.setEndDate(c.getTime());
	        	
	            list[i] = battle;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}*/
	
	public static BattleScore[] getBattleScores(int battle_id)
	{
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultList"; 
		String OPERATION_NAME = "resultList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetBattleScoresWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("batID", battle_id);
		
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        BattleScore[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;
	        list = new BattleScore[res.size()];
	        for (int i=0; i<res.size();i++){
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

		        BattleScore bs = new BattleScore();
		        bs.setBattleId(Integer.parseInt(so.getProperty(0).toString()));
	            bs.setPersonId(Integer.parseInt(so.getProperty(1).toString()));
	            //bs.setTotalScore(Long.parseLong(so.getProperty(2).toString()));
	            @SuppressWarnings("rawtypes")
				Vector scores = (Vector)so.getProperty(3);
	            for (int j=0; j<scores.size();j++){
	            	Object obj2 = (Object)scores.get(j);
			        SoapObject  so2 = (SoapObject)obj2;
			        
	            	bs.addDate(sdf.parse(so2.getProperty(0).toString()));
	            	bs.addScore(Long.parseLong(so2.getProperty(1).toString()));
	            }
	        	
	            list[i] = bs;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}
	
	public static boolean CreateUser(Context context) {
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/createUser"; 
		String OPERATION_NAME = "createUser";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/CreateUserWS.php";
		
		User user = FacebookFragment.loggedInUser;

		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("userID", user.getId());
		request.addProperty("email", user.getEmail());
		request.addProperty("name", user.getName());

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;

        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
        	Object obj = (Object)response;
	        boolean  res = (Boolean)obj;
	        
	        if (res != false) {
	        	Log.i(RestDataHandler.class.getSimpleName(), "CreateUser successful");
		        return true;
	        }
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return false;
	}
	
	public static ServerTime getServerTime() {
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultList"; 
		String OPERATION_NAME = "resultList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetServerTimeWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        ServerTime st = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        SoapObject  so = (SoapObject)response;
	        
	        if (so != null){
	        	st = new ServerTime();
	        	SimpleDateFormat  sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	        	
	        	st.setTimeZone(so.getProperty(0).toString());
	        	st.setCurrentDate(sdf.parse(so.getProperty(1).toString()));
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return st;
	}
	
	public static UserScore[] getUserScoreByPerson(int person_id) {
		String SOAP_ACTION = "http://schemas.xmlsoap.org/wsdl/resultList"; 
		String OPERATION_NAME = "resultList";
		String WSDL_TARGET_NAMESPACE = "http://schemas.xmlsoap.org/wsdl/";
		String SOAP_ADDRESS = "http://levelrewind.com/WS/GetUserScoreByPersonWS.php";
		
		SoapObject request = new SoapObject(WSDL_TARGET_NAMESPACE,
		        OPERATION_NAME);
		request.addProperty("perID", person_id);
		
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;
        envelope.setOutputSoapObject(request);
        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        httpTransport.debug = true;
        UserScore[] list = null;
        try
        {
	        httpTransport.call(SOAP_ACTION, envelope);
    
	        Object response = (Object)envelope.getResponse();
	        @SuppressWarnings("rawtypes")
			Vector res = (Vector)response;

        	list = new UserScore[res.size()];
	        for (int i=0; i<res.size();i++){
	        	
	        	Object obj = (Object)res.get(i);
		        SoapObject  so = (SoapObject)obj;

		        UserScore us = new UserScore();
		        us.setPersonID(Integer.parseInt(so.getProperty(0).toString()));
		        us.setUserID(Long.parseLong(so.getProperty(1).toString()));
		        us.setScore(Long.parseLong(so.getProperty(2).toString()));
	            list[i] = us;
	        } 
        }
        catch (Exception ex)
        {
        	ex.printStackTrace();
         
        }
        
        return list;
	}
	
	/*
	 * Download data from server to device
	 * Table : Category and Person
	 * */
	public boolean downloadData() {
		try {
			
			db.insertCategory(getCategories());
			
			int all_id = -1; //id < 0 for all persons
			db.insertPerson(getPersons(all_id)); 
			
			db.insertGame(getGames());

		}catch(Exception ex) {
			ex.printStackTrace();
			return false;
		}
		
		return true;
	}
	
	public Category[] getCategoriesFromDevice(){
		return db.getCategories();
	}
	
	public Person[] getPersonsFromDevice(int category_id){
		return db.getPersons(category_id);
	}
	
	public Game[] getGamesFromDevice(){
		return db.getGames();
	}
	
	public void updatePersonScoreOnDevice(int person_id, long score) {
		db.updatePersonScore(person_id, score);
	}
	
	public long getPersonScoreFromDevice(int person_id) {
		return db.getPersonScore(person_id);
	}
}
